package com.payroll.taglib.outliner;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.PageContext;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
/**
 * This tag is used to:<br/>
 * <li> Build the outliner grapically
 * <li> Build the outliner by code. If a vector of @see com.ams.sacwis.outliner.AMSOutlinerForm bean objects
 * are present in the request object, this calls @see com.ams.sacwis.outliner.AMSOutliner object to build the
 * rows corresponding to the attributes of the beans in the request object. <br/>
 * This tag also provides additional options to filter the data in the outliner. The following fields are passed
 * to the application action classes in the AMSOutlinerForm bean to process the corresponding filtered expansion.
 * <li> restrict - It is a flag to determine whether the radio buttons are visible or not. "true" means the restrict
 * option is set and the radio buttons are visible. The outliner infrastructure needs this flag to determine whether
 * to check for data in the radio buttons or not.
 * <li> option1id - The ID attribute for the first radio button
 * <li> option1text - The text to display for the first radio button
 * <li> option2id - The ID attribute for the second radio button
 * <li> option2text - The text to display for the second radio button
 * <li> option3id - The ID attribute for the first third button
 * <li> option3text - The text to display for the third radio button
 * <br/><br/> For example:
 *
 */
public class PayrollOutlinerPage extends TagSupport {
	private String restrict;
	private String option1id;
	private String option1text;
	private String option2id;
	private String option2text;
	private String option3id;
	private String option3text;
	private String option4id;
	private String option4text;

	public int doStartTag() throws JspException {
		ArrayList m_ArrayList =
			(ArrayList) pageContext.getAttribute("AMSOutlinerForm", PageContext.REQUEST_SCOPE);
		int i;
		try {
			generateViewOptions();
			pageContext.getOut().print("<DIV id=\"outlinerdiv\">");
			pageContext.getOut().print(
				"<TABLE class='outlinertable' name=\"outlinertable\" cellpadding=\"0\" cellspacing=\"0\"><TBODY>");
			if (m_ArrayList != null) {
				PayrollOutliner m_outliner = new PayrollOutliner();
				for (i = 0; i < m_ArrayList.size(); i++) {
					pageContext.getOut().print(
						m_outliner.createRow((PayrollOutlinerForm) m_ArrayList.get(i), ((HttpServletRequest) pageContext.getRequest()).getContextPath() ).toString());
				}
			}
		} catch (Exception ioException) {
			System.err.println("IO Exception thrown in code");
			System.err.println(ioException.toString());
			throw new JspException(ioException.toString());
		}
		return EVAL_BODY_INCLUDE;
	}
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().print("</TBODY></TABLE>");
			pageContext.getOut().print("</DIV>");
		} catch (Exception ioException) {
			System.err.println("IO Exception thrown in code");
			System.err.println(ioException.toString());
			throw new JspException(ioException.toString());
		}
		return EVAL_PAGE;
	}

	public void release() {
		super.release();
		restrict = null;
		option1id = null;
		option1text = null;
		option2id = null;
		option2text = null;
		option3id = null;
		option3text = null;
		option4id = null;
		option4text = null;

	}

	private void generateViewOptions() throws Exception {
		try {
			String m_visibility = "inline";
			if (restrict == "hidden" || restrict == "false")
				m_visibility = "none";

			pageContext.getOut().print(
				"<INPUT size='20' type='hidden' id='optionspresent' name='optionspresent' value='"
					+ restrict
					+ "'>");

			pageContext.getOut().print(
				"<TABLE title='Select view for outliner' class='outlineroptionstable' id='outlineroptions' name='outlineroptions'" +
				" style=\"display: " + m_visibility + "\">");
			pageContext.getOut().print("<TBODY>");
			pageContext.getOut().print("<TR class='outlineroptionsTR'>");

			if (!(option1id == "" || option1id == null)){
				pageContext.getOut().print("<TD><INPUT type='checkbox' checked='true' name='outlineroptionsgroup' id='option1' onclick='refreshOutliner();' value='" + option1id  + "' ></TD>");
				pageContext.getOut().print("<TD class='outlineroptions'>" + option1text  + "</TD>");
			}

			if (!(option2id == "" || option2id == null)){
				pageContext.getOut().print("<TD><INPUT type='checkbox' name='outlineroptionsgroup' id='option2' onclick='refreshOutliner();' value='" + option2id  + "' ></TD>");
				pageContext.getOut().print("<TD class='outlineroptions'>" + option2text  + "</TD>");
			}

			pageContext.getOut().print("</TR>");
			pageContext.getOut().print("</TBODY>");
			pageContext.getOut().print("</TABLE>");
		} catch (Exception ioException) {
			throw ioException;
		}
	}

	/**
	 * Gets the option1id
	 * @return Returns a String
	 */
	public String getOption1id() {
		return option1id;
	}
	/**
	 * Sets the option1id
	 * @param option1id The option1id to set
	 */
	public void setOption1id(String option1id) {
		this.option1id = option1id;
	}

	/**
	 * Gets the option1text
	 * @return Returns a String
	 */
	public String getOption1text() {
		return option1text;
	}
	/**
	 * Sets the option1text
	 * @param option1text The option1text to set
	 */
	public void setOption1text(String option1text) {
		this.option1text = option1text;
	}

	/**
	 * Gets the option2id
	 * @return Returns a String
	 */
	public String getOption2id() {
		return option2id;
	}
	/**
	 * Sets the option2id
	 * @param option2id The option2id to set
	 */
	public void setOption2id(String option2id) {
		this.option2id = option2id;
	}

	/**
	 * Gets the option2text
	 * @return Returns a String
	 */
	public String getOption2text() {
		return option2text;
	}
	/**
	 * Sets the option2text
	 * @param option2text The option2text to set
	 */
	public void setOption2text(String option2text) {
		this.option2text = option2text;
	}

	/**
	 * Gets the option3id
	 * @return Returns a String
	 */
	public String getOption3id() {
		return option3id;
	}
	/**
	 * Sets the option3id
	 * @param option3id The option3id to set
	 */
	public void setOption3id(String option3id) {
		this.option3id = option3id;
	}

	/**
	 * Gets the option3text
	 * @return Returns a String
	 */
	public String getOption3text() {
		return option3text;
	}
	/**
	 * Sets the option3text
	 * @param option3text The option3text to set
	 */
	public void setOption3text(String option3text) {
		this.option3text = option3text;
	}

	/**
	 * Gets the option4id
	 * @return Returns a String
	 */
	public String getOption4id() {
		return option4id;
	}
	/**
	 * Sets the option4id
	 * @param option4id The option4id to set
	 */
	public void setOption4id(String option4id) {
		this.option4id = option4id;
	}

	/**
	 * Gets the option4text
	 * @return Returns a String
	 */
	public String getOption4text() {
		return option4text;
	}
	/**
	 * Sets the option4text
	 * @param option4text The option4text to set
	 */
	public void setOption4text(String option4text) {
		this.option4text = option4text;
	}

	/**
	 * Gets the restrict
	 * @return Returns a String
	 */
	public String getRestrict() {
		return restrict;
	}
	/**
	 * Sets the restrict
	 * @param restrict The restrict to set
	 */
	public void setRestrict(String restrict) {
		this.restrict = restrict;
	}

}